package com.jxb.proxy.advisor;

import com.jxb.service.UserService;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.*;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
public class BojiaoPointcutAdvisor implements PointcutAdvisor {
    @Override
    public Pointcut getPointcut() {
        return new Pointcut() {
            @Override
            public ClassFilter getClassFilter() {
                return new ClassFilter() {
                    @Override
                    public boolean matches(Class<?> clazz) {
                        return clazz.getName().equals("com.jxb.service.UserService");
                    }
                };
            }

            @Override
            public MethodMatcher getMethodMatcher() {
                return new IntroductionAwareMethodMatcher() {
                    @Override
                    public boolean matches(Method method, Class<?> targetClass) {
                        return targetClass.getName().equals("com.jxb.service.UserService");
                    }

                    @Override
                    public boolean isRuntime() {
                        return true;
                    }

                    @Override
                    public boolean matches(Method method, Class<?> targetClass, Object... args) {
                        return targetClass.getName().equals("com.jxb.service.UserService");
                    }

                    @Override
                    public boolean matches(Method method, Class<?> targetClass, boolean hasIntroductions) {
                        return targetClass.getName().equals("com.jxb.service.UserService");
                    }
                };
            }
        };
    }

    @Override
    public Advice getAdvice() {
        return new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                System.out.println("before...");
                Object result = invocation.proceed();
                System.out.println("after...");
                return result;
            }
        };
    }

    @Override
    public boolean isPerInstance() {
        return false;
    }
}
